package com.xzkydz.ui;

import android.content.Intent;
import android.os.Build;
import android.os.Bundle;

import androidx.appcompat.app.AppCompatActivity;
import androidx.navigation.ui.AppBarConfiguration;

import com.xzkydz.sensor.sensor.JiGuangBean26;
import com.xzkydz.sensor.sensor.YaLiBean;
import com.xzkydz.sensor.sensor.base.BaseSensorData;
import com.xzkydz.sensor.service.SerialPortService;
import com.xzkydz.sensor.utils.CalculationUtils;
import com.xzkydz.serialport.databinding.ActivityMainBinding;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

public class MainActivity extends AppCompatActivity {

    private AppBarConfiguration appBarConfiguration;
    private ActivityMainBinding binding;

    private Intent intentSerialPort;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityMainBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        setSupportActionBar(binding.toolbar);
        EventBus.getDefault().register(this);
        iniEvent();
    }

    private void iniEvent() {
        //串口读取服务
        intentSerialPort = new Intent(MainActivity.this, SerialPortService.class);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            startForegroundService(intentSerialPort);
        } else {
            startService(intentSerialPort);
        }
    }


    @Subscribe(threadMode = ThreadMode.MAIN)
    public void event(BaseSensorData data) {
        //传感器数据
        if (data instanceof YaLiBean) {
            YaLiBean yaLiBean = (YaLiBean) data;
            StringBuffer sb = new StringBuffer();
            sb.append("串口包：" + CalculationUtils.byteArrToHex(data.bRec) + "\n")
//                    .append("压力数据：" + Arrays.toString(yaLiBean.yaLiArray) + "\n")
                    .append("压力：" + String.format("%.2f",yaLiBean.yaLiAver) + "\n")
                    .append("信号：" + yaLiBean.signalVel + "\n")
                    .append("电量:" + yaLiBean.powerVel + "\n");
            binding.content.tvShow.setText(sb.toString());

        }


        if (data instanceof JiGuangBean26) {
            JiGuangBean26 jiGuangBean26 = (JiGuangBean26) data;
            binding.content.tvShow.setText(CalculationUtils.byteArrToHex(data.bRec));
            StringBuffer sb = new StringBuffer();
            sb.append("串口包：" + CalculationUtils.byteArrToHex(data.bRec) + "\n")
                    .append("距离：" + jiGuangBean26.juliVel + "\n")
                    .append("信号：" + jiGuangBean26.signalVel + "\n")
                    .append("电量:" + jiGuangBean26.powerVel + "\n");
            binding.content.tvShow.setText(sb.toString());
        }

    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
        if (intentSerialPort != null) {
            stopService(intentSerialPort);
            intentSerialPort = null;
        }
    }

}